<?php
namespace app\admin\controller;
class Setting extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function about() {
        
    }
    
    public function updatePwd() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        $params = request()->post();
        if(empty($params)) {
            return errorResult();
        }
        
        $old = isset($params['password']) ? trim($params['password']) : '';
        if(strlen($old) != 32) {
            return errorResult('请输入原登录密码');
        }
        
        $newPwd = isset($params['newpwd']) ? trim($params['newpwd']) : '';
        if(strlen($newPwd) != 32) {
            return errorResult('请输入新登录密码');
        }
        
        $old = strtolower($old);
        $newPwd = strtolower($newPwd);
        
        if($old === $newPwd) {
            return errorResult('新旧密码不能一样');
        }
        
        $role = $this->role;
        $userId= $this->userid;
        
        $model = null;
        $where = [];
        if($role == 1) { //平台
            $model = model('Admin');
            $where['id'] = $userId;
        }elseif($role == 2) {
            $model = model('Agent');
            $where['agent_id'] = $userId;
        }elseif($role == 3) {
            $model = model('Store');
            $where['store_id'] = $userId;
        }
        
        $account = $model->findOne($where, ['password', 'salt']);
        if(empty($account)) {
            return errorResult();
        }
        
        if(md5(md5($old) . $account['salt']) !== $account['password']) {
            return errorResult('原登录密码错误');
        }
        
        $salt = uniqid();
        $salt = substr($salt, strlen($salt) -7 , 6);
        $newPwd = md5(md5($newPwd) . $salt);
        
        $rs = $model->modify($where, ['password' => $newPwd, 'salt' => $salt]);
        if(!$rs) {
            return errorResult('修改失败');
        }
        
        return successResult('设置成功');
    }
}










